<?php
/**
 * Created by PhpStorm.
 * User: 牛赞赞
 * Date: 2021/1/11
 * Time: 14:37
 */

namespace App\Task;

use App\Game\Conf\GameProgress;
use App\Game\Timer\GameTimer;
use App\Model\GameTaskDayComplete;
use App\Model\GameTaskDayList;
use App\Model\GameTaskGrowComplete;
use App\Model\GameTaskGrowList;
use App\Model\User;
use Hyperf\DbConnection\Db;
use App\Game\Core\Packet;
use App\Game\Conf\MainCmd;
use App\Game\Conf\SubCmd;
use App\Game\Core\DdzPoker;
use Psr\Container\ContainerInterface;
use Swoole\WebSocket\Server as WebSocketServer;

class PlayCardTask
{
    /**
     * @var ContainerInterface
     */
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    /**
     * 用户打牌逻辑处理
     * @param $user_room_data
     * @param $out_cards
     * @param $account
     * @param $fd
     * @throws \Exception
     */
    public function playCard($user_room_data, $inutData, $account, $fd): void
    {
        $serv = server();
        $out_cards = $inutData['card'];
        if (!$out_cards) {
            $msg = array('status' => 1, 'info' => '出牌数据不能为空！');
            $this->gameOutCard($serv, $fd, $msg);
            return;
        }

        $room_no = json_decode($user_room_data['room_no'], true);
        if (!checkGameProgress($room_no, GameProgress::GAME_OUT_CARD)) {
            $msg = array('status' => 1, 'info' => '出牌非法, 还未到出牌阶段！');
            $this->gameOutCard($serv, $fd, $msg);
            return;
        }

        //检查操作是否已经超时
        if (!checkCounter($room_no)) {
            $msg = ['status' => 1, 'info' => '该操作已超时，系统已自动为您执行默认操作'];
            $this->gameOutCard($serv, $fd, $msg);
            return;
        }

        //检查出的牌是否在用户手牌中
        $user_data = json_decode($user_room_data[$account], true);
        if (!in_array($out_cards, $user_data['card'])) {
            $msg = ['status' => 1, 'info' => '出牌非法，出牌数据不存在'];
            $this->gameOutCard($serv, $fd, $msg);
            return;
        }

        //手次
        $hand_num = isset($user_room_data['hand_num']) ? $user_room_data['hand_num'] + 1 : 1;
        $master = json_decode($user_room_data['master'], true);
        //首次出牌，判断是否为庄家出牌
        $chair_id = findChairId($room_no, $account); // 座位号
        if ($hand_num == 1) {
//            dd('out_cards[chair_id]', $out_cards['chair_id']);
//            dd('master', $master);
            if ($chair_id != $master) {
                $msg = array('status' => 1, 'info' => '出牌非法, 当前为庄家出牌');
                $this->gameOutCard($serv, $fd, $msg);
                return;
            }
        }
        if (isset($user_room_data['next_chair_id']) && $user_room_data['next_chair_id'] != $chair_id) {
            $msg = array('status' => 2, 'info' => '出牌非法, 还未轮到您出牌');
            $this->gameOutCard($serv, $fd, $msg);
            return;
        }
        //判断总手牌数，是否存在重复出牌
        $totalCardNum = count($user_data['card']);
        $gangNum = isset($user_data['gang']) ? count($user_data['gang']) : 0;
        if ($gangNum == 0 && $totalCardNum < 14) {
            $msg = array('status' => 3, 'info' => '出牌非法，重复出牌');
            $this->gameOutCard($serv, $fd, $msg);
            return;
        }
        if ($gangNum == 1 && $totalCardNum < 15) {
            $msg = array('status' => 3, 'info' => '出牌非法，重复出牌');
            $this->gameOutCard($serv, $fd, $msg);
            return;
        }
        if ($gangNum == 2 && $totalCardNum < 16) {
            $msg = array('status' => 3, 'info' => '出牌非法，重复出牌');
            $this->gameOutCard($serv, $fd, $msg);
            return;
        }
        if ($gangNum == 3 && $totalCardNum < 17) {
            $msg = array('status' => 3, 'info' => '出牌非法，重复出牌');
            $this->gameOutCard($serv, $fd, $msg);
            return;
        }
        if ($gangNum == 4 && $totalCardNum < 18) {
            $msg = array('status' => 3, 'info' => '出牌非法，重复出牌');
            $this->gameOutCard($serv, $fd, $msg);
            return;
        }

        //验证出牌数据
        if (!in_array($out_cards, config('mj'))) {
            $msg = array('status' => 3, 'info' => '出牌非法, 请出手牌');
            $this->gameOutCard($serv, $fd, $msg);
            return;
        }

        //设置出牌缓存
        updateRoomData($room_no, ['out' => ['card' => $out_cards, 'chair_id' => $chair_id]]);
        ddTitle('do_out_card 0000000000 ');
        //清除前一个定时器
//        container()->get(GameTimerTask::class)->clean($room_no);
        cleanTimer($room_no);
        GameTimer::do_out_card($room_no);
    }


    /**
     * 向客户端当前玩家发送错误提示响应, 单发
     * @param $serv
     * @param $fd
     * @param $param
     * @return bool
     */
    protected function gameOutCard($serv, $fd, $param)
    {
        $data = Packet::packFormat('OK', 0, $param);
        $data = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmd::SUB_GAME_OUT_CARD_RESP);
        $serv->push($fd, $data, WEBSOCKET_OPCODE_BINARY);
        return false;
    }
}